#include <string>
#include <stack>
#include <iostream>

using std::string;
using std::stack;

class Solution {
public:
    string simplifyPath(string path) {
        int left = 0;
        int right = 0;
        stack<string> stack;
        path += '/';
        do {
            right = path.find('/', left + 1);
            if (right == string::npos) {
                break;
            }
            else if (right > left + 1) {
                string tmp = path.substr(left + 1, right - left - 1);
                if (tmp == "..") {
                    if (!stack.empty()) {
                        stack.pop();
                    }
                }
                else if (tmp != ".") {
                    stack.push(tmp);
                }
            }
            left = right;
        } while (left < path.size() - 1);
        if (stack.empty()) {
            return "/";
        }
        else {
            string ret = "";
            while (!stack.empty()) {
                ret = "/" + stack.top() + ret;
                stack.pop();
            }
            return ret;
        }
    }
};